Passed
Push — main ( 0c6882...1f9a40 )
by Stefan
03:05
created

FormGenerator.js ➔ loadScript   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
1
/** global: g_oConfigFromPHP */
2
/** global: FormDataValidator */
3
/** global: jscolor */
4
/** global: CKEDITOR */
5
/** global: FormCKEditor */
6
/** global: RichFmConnector */
7
/** global: FormPicker */
8
9
var g_oCKEdit = null;
10
var g_FDP = null;
11
12
/**
13
 * post load some additional JS scripts after DOM is loaded...
14
 */
15
document.addEventListener('DOMContentLoaded', loadScriptfiles);
16
17
function loadScriptfiles()
18
{
19
    loadScript(g_oConfigFromPHP.JavaScript.Path + 'FormDataValidator.js');
20
    if (g_oConfigFromPHP.Color !== undefined) {
21
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'jscolor.js');
22
    }
23
    
24
    if (g_oConfigFromPHP.DTSel !== undefined) {
25
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'dtsel.js');
26
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'FormPicker.js');
27
    }
28
29
    if (g_oConfigFromPHP.RichFilemanager !== undefined) {
30
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'RichFmConnector.js');
31
    }
32
    
33
    if (g_oConfigFromPHP.CKEditor !== undefined) {
34
        loadScript(g_oConfigFromPHP.CKEditor.Path + 'ckeditor.js');
35
        loadScript(g_oConfigFromPHP.JavaScript.Path + 'FormCKEdit.js');
36
    }
37
}
38
39
/**
40
 */
41
42
/**
43
 * Initialization.
44
 * For initialization we lisen to the window onload-event. At this point the 
45
 * additionally loaded script files from the DOMContentLoaded-event should also be 
46
 * available. 
47
 * Dependent on the config from PHP we have to initialize some modules:
48
 * - FormDataValidation needs no initialization
49
 * - if form contains colorpicker(s) we have to initialize them
50
 * - contained date or time pickers also needs initialization
51
 * - embedded CKEditor have to be loaded and configured
52
 */
53
window.addEventListener('load', initFormGenerator);
54
55
function initFormGenerator()
56
{
57
    if (g_oConfigFromPHP.Color !== undefined) {
58
        jscolor.presets.default = g_oConfigFromPHP.Color;
59
        jscolor.init();
60
    }
61
    
62
    if (g_oConfigFromPHP.DTSel !== undefined) {
63
    	g_FDP = new FormPicker(g_oConfigFromPHP);
64
        g_FDP.init();
65
    }
66
    
67
    if (g_oConfigFromPHP.CKEditor !== undefined) {
68
        g_oCKEdit = new FormCKEditor(g_oConfigFromPHP, CKEDITOR);
69
        g_oCKEdit.load();
70
    }
71
}
72
73
/**
74
 * Helper function to create Error/Warning-Message in the document.
75
 * The Messeage is only created, if the debug mode is enabled.
76
 * @param string msg text to display
77
 * @param string level level for the message
78
 */
79
function displayJSError(msg, level)
80
{
81
    if (g_oConfigFromPHP.DebugMode) {
82
        let div = document.createElement('div');
83
        div.id = 'JSError';
84
        let header = document.createElement('h1');
85
        div.appendChild(header);
86
        let body = document.createElement('p');
87
        div.appendChild(body);
88
        header.innerHTML = 'Javascript ' + level;
89
        body.innerHTML = msg;
90
        document.body.insertBefore(div, document.body.firstChild);
91
    }
92
}
93
94
/**
95
 * Validate the form.
96
 */
97
function validateForm()
98
{
99
    var FDV = new FormDataValidator(g_oConfigFromPHP.FormDataValidation);
100
    return FDV.validate();
101
}
102
103
/**
104
 * Call the filemanager to browse for a file on the server.
105
 */
106
function browseServer(editID, imgID, strExpand)
107
{
108
    let FmConnector = new RichFmConnector(g_oConfigFromPHP.RichFilemanager.Path);
109
    FmConnector.editID = editID;
110
    FmConnector.imgID = imgID;
111
    FmConnector.browseServerModal(strExpand);
112
}
113
114
/**
115
 * Handler for the DTU button (Date,Time User)
116
 */
117
function onInsertDateTimeUser(id, strUsername)
118
{
119
    let oEdit = document.getElementById(id);
120
    if (oEdit) {
121
        let date = new Date();
122
        let strValue = oEdit.innerHTML = date.toLocaleString();
123
        if (strUsername != '') {
124
            strValue += ' / ' + strUsername;
125
        }
126
        oEdit.value = strValue;
127
    }
128
}
129
130
/**
131
 * Handler for the reset - Button.
132
 * Used to to reset the content of readonly-inputs that get the value
133
 * from filebrowser or anather picker
134
 */
135
function resetInput(id)
136
{
137
    let oEdit = document.getElementById(id);
138
    if (oEdit) {
139
        oEdit.value = '';
140
    }
141
}
142
143
/**
144
 * Dynamic loading of additional scripts.
145
 */
146
function loadScript(strScriptfile)
147
{
148
    var oScript = document.createElement('script');
149
    document.head.appendChild(oScript);
150
    // oScript.onload = function() {console.log('script ' + strScriptfile + ' loaded!');};
151
    oScript.type = 'text/javascript';
152
    oScript.src = strScriptfile;
153
}
154
155